package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Zlrz;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class ZlrzForm extends BasePage implements Serializable {
    private GenericManager<Zlrz, Long> zlrzManager;
    private Zlrz zlrz = new Zlrz();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setZlrzManager(GenericManager<Zlrz, Long> zlrzManager) {
        this.zlrzManager = zlrzManager;
    }

    public Zlrz getZlrz() {
        return zlrz;
    }

    public void setZlrz(Zlrz zlrz) {
        this.zlrz = zlrz;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        zlrzManager.remove(zlrz.getId());
        addMessage("zlrz.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            zlrz = zlrzManager.get(id);
        } else {
            zlrz = new Zlrz();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (zlrz.getId() == null || zlrz.getId() == 0);
        zlrz.setJbqk( jbqk );
        zlrz.setUsername(getRequest().getRemoteUser());
        zlrz.setOptdate(new Date());
        zlrz.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        zlrzManager.save(zlrz);

        String key = (isNew) ? "zlrz.added" : "zlrz.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = zlrz.getId();
            return "edit";
        }
    }
} 